/**
 * Created by paul on 09/05/14.
 */
/**
 * Created by paul on 09/05/14.
 */
BasicGame.MapLevel = function (game) {
    this.Map;
    this.BackgroundLayer;
    this.CollisionLayer;
    this.Player;
    this.Cursors;

    this.EnterKey;

    this.startBattle = function() {
        this.game.state.start('Battle', true, false);
    }
};

BasicGame.MapLevel.prototype = {

    preload: function () {
        console.log('MapLevel is alive');

        game.load.tilemap('mapLevel', 'maps/map.json', null, Phaser.Tilemap.TILED_JSON);
        game.load.image('TheTileSet', 'sprites/GSCTileset32x32.png');
        game.load.spritesheet('player', 'sprites/player.png', 32, 32);
    },

    create: function () {
        console.log('MapLevel create');

        game.physics.startSystem(Phaser.Physics.ARCADE);

        this.Map = game.add.tilemap("mapLevel");
        this.Map.addTilesetImage('TheTileSet');

        this.BackgroundLayer = this.Map.createLayer('BackgroundLayer');
        this.CollisionLayer = this.Map.createLayer('CollisionLayer');
        game.physics.arcade.enable(this.CollisionLayer);
        this.Map.setCollisionByExclusion([],true,this.CollisionLayer);
        //this.CollisionLayer.debug = true;

        //this.Map.setCollision(40);


        this.Player = game.add.sprite(128, 128, 'player');
        game.physics.enable(this.Player, Phaser.Physics.ARCADE);

        this.Player.body.collideWorldBounds = true;

        game.camera.follow(this.Player);

        //this.Player.body.setSize(20, 32, 5, 16);

        this.Cursors = game.input.keyboard.createCursorKeys();

        this.EnterKey = game.input.keyboard.addKey(Phaser.Keyboard.Q);
        this.EnterKey.onDown.addOnce(this.startBattle, this);

    },

    update: function() {
        game.physics.arcade.collide(this.Player, this.CollisionLayer);

        //  Un-comment these to gain full control over the sprite
        this.Player.body.velocity.x = 0;
        this.Player.body.velocity.y = 0;

        if (this.Cursors.up.isDown)
        {
            this.Player.body.velocity.y = -150;
        }
        else if (this.Cursors.down.isDown)
        {
            this.Player.body.velocity.y = 150;
        }

        if (this.Cursors.left.isDown)
        {
            this.Player.body.velocity.x = -150;
        }
        else if (this.Cursors.right.isDown)
        {
            this.Player.body.velocity.x = 150;
        }



    },

    render: function() {

    }
};
